package com.xiong.wuliao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.xiong.wuliao.entity.Urls;
import com.xiong.wuliao.util.HomeJSON;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

	private ImageButton myRegisterButtonGoodBack; // 返回按钮
	private EditText myRegisterNikeNameEditText; // 昵称
	private EditText myRegisterPhotoEditText; // 手机号
	private EditText myRegisterPassWordEditText; // 密码
	private RelativeLayout myRegisterRegisterLayout; // 注册
	boolean re = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		init();
	}

	private void init() {
		// 初始化View
		initView();
		setListener();
	}

	private void setListener() {
		myRegisterButtonGoodBack.setOnClickListener(this);
		myRegisterRegisterLayout.setOnClickListener(this);
	}

	private void initView() {
		myRegisterButtonGoodBack = (ImageButton) findViewById(R.id.myRegisterButtonGoodBack);
		myRegisterNikeNameEditText = (EditText) findViewById(R.id.myRegisterNikeNameEditText);
		myRegisterPhotoEditText = (EditText) findViewById(R.id.myRegisterPhotoEditText);
		myRegisterPassWordEditText = (EditText) findViewById(R.id.myRegisterPassWordEditText);
		myRegisterRegisterLayout = (RelativeLayout) findViewById(R.id.myRegisterRegisterLayout);
	}

	/**
	 * 注册操作，成功则跳转到登录界面
	 */
	private void register() {
		String userName = myRegisterNikeNameEditText.getText().toString();
		String phone = myRegisterPhotoEditText.getText().toString();
		String passWord = myRegisterPassWordEditText.getText().toString();
		int u = userName.length();
		int ph = phone.length();
		int pa = passWord.length();
		if (0 < u && u < 8 && ph == 11 && 6 <= pa && pa <= 12) {
			OkHttpUtils.get(Urls.RegisterPath)
					.params("userName",userName)
					.params("passWord",passWord)
					.params("phone",phone)
					.execute(new StringCallback() {
						@Override
						public void onSuccess(String s, Call call, Response response) {
							JSONObject jsonObject = null;
							try {
								jsonObject = new JSONObject(s.trim());
								String result = jsonObject.getString("message");
								if (result.equals("OK")) {
									Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
									// 注册成功
									Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
									startActivity(intent);
									RegisterActivity.this.finish();
								} else {
									// 服务器验证失败（已经存在该用户）
									Toast.makeText(RegisterActivity.this, "该用户已存在", Toast.LENGTH_SHORT).show();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					});
		}else {
			// 用户名、手机号、密码错误
			Toast.makeText(RegisterActivity.this, "用户名、手机号或密码错误", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 请求服务器注册操作
	 * @param userName
	 * @param phone
	 * @param passWord
	 * @return
	 */
	private void registerInServer(String userName, String phone, String passWord) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			// 返回
			case R.id.myRegisterButtonGoodBack:
				RegisterActivity.this.finish();
				break;
			// 注册
			case R.id.myRegisterRegisterLayout:
				register();
				break;
		}
	}
}
